﻿using BBCore.Application.Service;
using BBCore.Core.Models.Samples;
using BBCore.Infrastructure.Helps;
using ClosedXML.Excel;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBCore.Application.Interface
{
    /// <summary>
    /// 使用Closedxml导出单表或者主从表
    /// </summary>
    public interface IClosedxmlExcelExportService
    {
        /// <summary>
        /// 导出单个工作表的 Excel 文件。
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="data">要导出的数据列表</param>
        /// <param name="sheetName">工作表名称</param>
        /// <param name="columns">列头及选择器元组，包含列头名称和数据选择器</param>
        /// <returns>返回生成的 Excel 工作簿</returns>
        IXLWorkbook ExportSingleSheet<T>(
            List<T> data,
            string sheetName,
            List<(string Header, Func<T, object> Selector)> columns);

        /// <summary>
        /// 导出包含主表和明细的 Excel 文件，支持 Master-Detail 数据结构。
        /// </summary>
        /// <typeparam name="TMaster">主数据类型</typeparam>
        /// <typeparam name="TDetail">明细数据类型</typeparam>
        /// <param name="masters">主数据列表</param>
        /// <param name="detailSelector">从主数据获取明细数据的选择器函数</param>
        /// <param name="sheetName">工作表名称</param>
        /// <param name="masterColumns">主表列头及选择器元组</param>
        /// <param name="detailColumns">明细表列头及选择器元组</param>
        /// <returns>返回生成的 Excel 工作簿</returns>
        IXLWorkbook ExportMasterDetail<TMaster, TDetail>(
            List<TMaster> masters,
            Func<TMaster, List<TDetail>> detailSelector,
            string sheetName,
            List<(string Header, Func<TMaster, object>)> masterColumns,
            List<(string Header, Func<TDetail, object>)> detailColumns);

        /// <summary>
        /// 自动导出主从结构（Master-Detail）的 Excel 表格，无需手动指定列头和选择器。
        /// </summary>
        /// <typeparam name="TMaster">主表实体类型，例如 PurchaseOrder。</typeparam>
        /// <typeparam name="TDetail">明细表实体类型，例如 OrderDetail。</typeparam>
        /// <param name="masters">主表数据列表。</param>
        /// <param name="detailSelector">
        /// 用于从主表对象中提取明细数据的函数，通常为 <c>o => o.Details</c>。
        /// </param>
        /// <param name="sheetName">工作表名称。</param>
        /// <returns>返回构建好的 Excel 工作簿（IXLWorkbook）。</returns>
        /// <remarks>
        /// - 自动获取 <typeparamref name="TMaster"/> 和 <typeparamref name="TDetail"/> 的属性作为列；
        ///— 如果属性有<see cref="System.ComponentModel.DataAnnotations.DisplayAttribute"/>，将使用其中的<c> Name</c> 作为列头；
        ///— 否则默认使用属性名称；
        ///— 主表中包含集合类型的属性（如 List<OrderDetail>）将被忽略，仅导出基本字段。
        /// </remarks>
        IXLWorkbook ExportMasterDetailAuto<TMaster, TDetail>(
            List<TMaster> masters,
            Func<TMaster, List<TDetail>> detailSelector,
            string sheetName);

    }
}

/* 示例:
var lst = freeSql.Select<HYJ_Master_Table>().IncludeMany(a => a.Details).ToList();
//using IXLWorkbook workbook = ClosedxmlExcelExportService.ExportMasterDetailAuto(lst, o => o.Details!, "结果");
//await _export.ExportAsExcelAsync(JSRuntime, workbook, "结果", MimeType.Excel);
// 定义主表列头和明细表列头
var masterColumns = new List<(string Header, Func<HYJ_Master_Table, object> Selector)>
              {
                  ("JOB_NO", order => order.JOB_NO),
                  ("日期", order => order.REQ_DATE.ToString("yyyy-MM-dd")),
                  ("数量", order => order.QUANTITY)
              };

var detailColumns = new List<(string Header, Func<HYJ_Detail_Table, object> Selector)>
              {
                  ("部品号", detail => detail.ITEM!),
                  ("LEVEL2", detail => detail.LEVEL2!)

              };
using IXLWorkbook workbook = ClosedxmlExcelExportService.ExportMasterDetail(
     lst,
     order => order.Details!,  // 明细选择器
     "测试",
     masterColumns,
     detailColumns
 );
await _export.ExportAsExcelAsync(JSRuntime, workbook, "结果", MimeType.Excel);
*/
